<template>
    <link href="/assets/css/reset.css" rel="stylesheet" />
    <link rel="stylesheet" type="text/css" href="/assets/css/login.css" />
    <div class="limiter">
        <div class="container-login100">
            <div class="wrap-login100">
                <van-form @submit="onSubmit">
                    <div class="login100-form-avatar">
                        <img src="/assets/images/pexels-spencer-selover-428364.jpg" />
                    </div>
                    <van-cell-group inset>
                        <van-field
                        v-model="mobile"
                        name="mobile"
                        placeholder="请输入手机号"
                        :rules="mobileRule"
                        />
                        <van-field
                        v-model="password"
                        type="password"
                        name="password"
                        placeholder="请输入密码"
                        :rules="passwordRule"
                        />
                    </van-cell-group>
                    <div style="margin: 16px;">
                        <van-button round block type="primary" native-type="submit">
                        登录
                        </van-button>
                    </div>
                    <div class="action">
                        <router-link to="/business/register" class="txt1">无账号?立即注册 》</router-link>
                    </div>
                </van-form>
            </div>
        </div>
    </div>
</template>

<script setup>
    import cookie from "vue-cookies"

    let {proxy} = getCurrentInstance()

    let mobile = ref("");
    let password = ref("");

    let mobileRule = [
        {
            required:true,message:"请填写手机号"
        },
        {
            pattern:/(^1[3|4|5|7|8]\d{9}$)/,message:"手机号格式不正确"
        }
    ]

    let passwordRule = [
        {
            required:true,message:"请填写密码"
        },
        {
            pattern:/\w{8,15}$/,message:"密码长度在8到15位之间"
        }
    ]

    let onSubmit = async function(values){
        let result = await proxy.$POST({
            url:"business/login",
            params:values,
        })
        if(result.code){
            // 设置cookie
            // proxy.$cookies.set("mobile",result.data.mobile)
            // proxy.$cookies.set("id",result.data.id)
            
            proxy.$cookies.set("business",result.data)
            proxy.$success({
                message:result.msg,
                duration:1000,
                onClose:()=>{
                    proxy.$router.push(result.url)
                    return false
                }
            })
        }else{
            proxy.$fail(result.msg)
            return false
        }
    }
 

  

</script>